package com.bpf.base.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bpf.base.common.Constants;
import com.bpf.base.controller.BaseController;
import com.bpf.base.model.system.OperateLog;
import com.bpf.base.service.system.OperateLogService;
import com.bpf.base.util.AjaxUtils;

@Controller
@RequestMapping(value = "/system/operateLog")
public class OperateLogController extends BaseController {

	@Autowired
	private OperateLogService operateLogService;

	@RequestMapping(value = { "/init", "" })
	public String init() {
		return "system/log";
	}

	/**
	 * 查看日志
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) {
		Integer pageNum = Integer
				.parseInt(null == request.getParameter("page") ? 1 + ""
						: request.getParameter("page").toString());
		Integer pageSize = Integer.parseInt(null == request
				.getParameter("rows") ? 10 + "" : request.getParameter("rows")
				.toString());
		String type= request.getParameter("type");
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put(Constants.Page.OFFSET, (pageNum - 1) * pageSize);
		conditions.put(Constants.Page.LIMIT, pageSize);
		if(type != null && !"".equals(type)){
			conditions.put("type", type);
		}
		conditions.put(Constants.Condition.ORDERBY_CLAUSE,
				"OPERATE_CREATE_TIME DESC");
		List<OperateLog> list = operateLogService.selectList(conditions);
		int total = operateLogService.selectCount(conditions);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 清空日志
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/deleteAll")
	@ResponseBody
	public Map<String, Object> deleteAll() {

		if (operateLogService.deleteByExample(null) > 0) {
			return AjaxUtils.reponseToJson(Constants.Action.DELETE, true);
		}
		return AjaxUtils.reponseToJson(Constants.Action.DELETE, false);

	}

}
